/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/*
 * Andrew Wells
 * Nov 5th, 2011
 * 
 * This activity is in charge of the creation and returning of a character that meets all the requirements
 * this app needs to successfully keep track of a character's information
 * 
 * It is initialized without any prior informaiton and returns a character object completely filled out.
 * 
 * The use of visual queues (progress bars and checkmarks) allow for an easy character creation experience
 * 
 */

public class CreateCharacter extends Activity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Character character = new Character();
	Bundle myChar = new Bundle();

	Intent myLocalIntent;

	boolean raceSelected = false;
	boolean classSelected = false;

	EditText STRTxt;
	EditText DEXTxt;
	EditText CONTxt;
	EditText INTTxt;
	EditText WISTxt;
	EditText CHATxt;

	EditText nameText;

	TextView raceModTxt;
	TextView classModTxt;

	Button raceSelectButton;
	Button classSelectButton;
	Button createButton;

	Button STRM;
	Button STRP;

	Button DEXM;
	Button DEXP;

	Button CONM;
	Button CONP;

	Button INTM;
	Button INTP;

	Button WISM;
	Button WISP;

	Button CHAM;
	Button CHAP;

	String charRace;
	String charClass;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_character2);

		//Initialize the EditText elements
		STRTxt = (EditText) findViewById(R.id.createEditSTR);
		DEXTxt = (EditText) findViewById(R.id.createEditDEX);
		CONTxt = (EditText) findViewById(R.id.createEditCON);
		INTTxt = (EditText) findViewById(R.id.createEditINT);
		WISTxt = (EditText) findViewById(R.id.createEditWIS);
		CHATxt = (EditText) findViewById(R.id.createEditCHA);

		nameText = (EditText) findViewById(R.id.CreateTraitsName);

		raceModTxt = (TextView) findViewById(R.id.createRaceModTxt);
		classModTxt = (TextView) findViewById(R.id.createClassModTxt);

		//Lots of button initializaion and assignining...

		raceSelectButton = (Button) findViewById(R.id.CreateRaceButton);
		classSelectButton = (Button) findViewById(R.id.CreateClassButton);
		createButton = (Button) findViewById(R.id.buttonCreateCharacter);

		STRM = (Button) findViewById(R.id.createSTRM);
		STRP = (Button) findViewById(R.id.createSTRP);

		DEXM = (Button) findViewById(R.id.createDEXM);
		DEXP = (Button) findViewById(R.id.createDEXP);

		CONM = (Button) findViewById(R.id.createCONM);
		CONP = (Button) findViewById(R.id.createCONP);

		INTM = (Button) findViewById(R.id.createINTM);
		INTP = (Button) findViewById(R.id.createINTP);

		WISM = (Button) findViewById(R.id.createWISM);
		WISP = (Button) findViewById(R.id.createWISP);

		CHAM = (Button) findViewById(R.id.createCHAM);
		CHAP = (Button) findViewById(R.id.createCHAP);

		//Map all the buttons to variables to be used with click listeners
		initCreateMenu();

		myLocalIntent = getIntent();
	}

	//Function to determine if strings passed are numbers
	public boolean isNumber(String num) {
		String numToken = "[[-]?\\d]+";
		if (num.matches(numToken))
			return true;
		else
			return false;
	}

	//Map all the buttons to variables to be used with click listeners
	private void initCreateMenu() {

		//Sets the EditTexts to be non editable
		STRTxt.setKeyListener(null);
		DEXTxt.setKeyListener(null);
		CONTxt.setKeyListener(null);
		INTTxt.setKeyListener(null);
		WISTxt.setKeyListener(null);
		CHATxt.setKeyListener(null);

		final EditText STRTxt = (EditText) findViewById(R.id.createEditSTR);
		final EditText DEXTxt = (EditText) findViewById(R.id.createEditDEX);
		final EditText CONTxt = (EditText) findViewById(R.id.createEditCON);
		final EditText INTTxt = (EditText) findViewById(R.id.createEditINT);
		final EditText WISTxt = (EditText) findViewById(R.id.createEditWIS);
		final EditText CHATxt = (EditText) findViewById(R.id.createEditCHA);

		//Sets the EditTexts to be non editable
		STRTxt.setKeyListener(null);
		DEXTxt.setKeyListener(null);
		CONTxt.setKeyListener(null);
		INTTxt.setKeyListener(null);
		WISTxt.setKeyListener(null);
		CHATxt.setKeyListener(null);

		//Lots of button initializaion and assignining...
		Button STRM = (Button) findViewById(R.id.createSTRM);
		Button STRP = (Button) findViewById(R.id.createSTRP);

		Button DEXM = (Button) findViewById(R.id.createDEXM);
		Button DEXP = (Button) findViewById(R.id.createDEXP);

		Button CONM = (Button) findViewById(R.id.createCONM);
		Button CONP = (Button) findViewById(R.id.createCONP);

		Button INTM = (Button) findViewById(R.id.createINTM);
		Button INTP = (Button) findViewById(R.id.createINTP);

		Button WISM = (Button) findViewById(R.id.createWISM);
		Button WISP = (Button) findViewById(R.id.createWISP);

		Button CHAM = (Button) findViewById(R.id.createCHAM);
		Button CHAP = (Button) findViewById(R.id.createCHAP);

		//Each of these are the listeners for the plus and minus buttons (pretty self-explanatory)
		//----------------------------------
		STRM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(STRTxt.getText().toString()) - 1;
				STRTxt.setText(String.valueOf(val));
			}
		});
		STRP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(STRTxt.getText().toString()) + 1;
				STRTxt.setText(String.valueOf(val));
			}
		});
		//----------------------------------
		DEXM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(DEXTxt.getText().toString()) - 1;
				DEXTxt.setText(String.valueOf(val));
			}
		});
		DEXP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(DEXTxt.getText().toString()) + 1;
				DEXTxt.setText(String.valueOf(val));
			}
		});
		//----------------------------------
		CONM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(CONTxt.getText().toString()) - 1;
				CONTxt.setText(String.valueOf(val));
			}
		});
		CONP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(CONTxt.getText().toString()) + 1;
				CONTxt.setText(String.valueOf(val));
			}
		});
		//----------------------------------
		INTM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(INTTxt.getText().toString()) - 1;
				INTTxt.setText(String.valueOf(val));
			}
		});
		INTP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(INTTxt.getText().toString()) + 1;
				INTTxt.setText(String.valueOf(val));
			}
		});
		//----------------------------------
		WISM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(WISTxt.getText().toString()) - 1;
				WISTxt.setText(String.valueOf(val));
			}
		});
		WISP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(WISTxt.getText().toString()) + 1;
				WISTxt.setText(String.valueOf(val));
			}
		});
		//----------------------------------
		CHAM.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(CHATxt.getText().toString()) - 1;
				CHATxt.setText(String.valueOf(val));
			}
		});
		CHAP.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int val = Integer.parseInt(CHATxt.getText().toString()) + 1;
				CHATxt.setText(String.valueOf(val));
			}
		});

		raceSelectButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				chooseRace();
			}

			public void chooseRace() {
				final CharSequence[] items = { "Human", "Dwarf", "Elf", "Gnome", "Halfling", "Half-elf", "Half-orc" };
				AlertDialog.Builder dialog = new AlertDialog.Builder(CreateCharacter.this); // Dialog(Add.this);
				dialog.setTitle("Select your race");
				dialog.setItems(items, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int item) {
						raceSelectButton.setText(items[item]);
						raceSelected = true;

						charRace = raceSelectButton.getText().toString();

						String modStr = character.abilities.getModChoiceString(items[item].toString());

						raceModTxt.setText(modStr);

					}
				});
				AlertDialog alert = dialog.create();
				alert.show();
			}
		});

		classSelectButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				chooseClass();
			}

			public void chooseClass() {
				final CharSequence[] items = { "Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Wizard" };
				AlertDialog.Builder dialog = new AlertDialog.Builder(CreateCharacter.this);
				dialog.setTitle("Select your race");
				dialog.setItems(items, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int item) {
						classSelectButton.setText(items[item]);
						classSelected = true;

						charClass = classSelectButton.getText().toString();

						classModTxt.setText("See book for list of class abilities");

					}
				});
				AlertDialog alert = dialog.create();
				alert.show();
			}
		});

		//Creates a dialogue box for entering a Name, Race and Class of the character
		createButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				//Checking the contents of the data fields
				if (nameText.getText().length() > 0 && classSelected == true && raceSelected == true) {

					int[] rawAbilityScores = { Integer.parseInt(STRTxt.getText().toString()), Integer.parseInt(DEXTxt.getText().toString()), Integer.parseInt(CONTxt.getText().toString()),
							Integer.parseInt(INTTxt.getText().toString()), Integer.parseInt(WISTxt.getText().toString()), Integer.parseInt(CHATxt.getText().toString()) };

					character.abilities.initializeCharacterAbilities(charRace, rawAbilityScores);

					//If it is successful, assign the character's traits to the data entered
					character.initializeCharacter(nameText.getText().toString(), charRace, charClass);

					//character.abilities.applyRaceMods(charRace);

					myChar.putSerializable("character", character);
					myLocalIntent.putExtras(myChar);

					setResult(Activity.RESULT_OK, myLocalIntent);
					finish();
				} else {

					//Removes the character's traits
					classSelectButton.setText("Select");
					raceSelectButton.setText("Select");

					classModTxt.setText("");
					raceModTxt.setText("");

					Toast.makeText(getApplicationContext(), "Please enter Name, Abilities, Race, and Class", Toast.LENGTH_LONG).show();
				}

			}
		});

	}
}